﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using System.Data;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Text.RegularExpressions;
public partial class Manage_Emp3_ReportH : AdminPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // 正确的属性设置方法
            this.GridView1.Attributes.Add("SortExpression", "ClassId");
            this.GridView1.Attributes.Add("SortDirection", "ASC");
            DataTable dt = new DataTable();
            dt.Columns.Add("ClassId");
            dt.Columns.Add("Code");
            dt.Columns.Add("ClassTypeId");
            dt.Columns.Add("ClassTypeName");
            dt.Columns.Add("Name");


            GridviewControl.GridViewDataBind(this.GridView1, dt);
            GridviewControl.ResetGridView(this.GridView1);

           
            //this.GridView2.Attributes.Add("SortExpression", "CenterId");
            //this.GridView2.Attributes.Add("SortDirection", "ASC");
            //DataTable dt1 = new DataTable();
            //dt1.Columns.Add("CenterId");
            //dt1.Columns.Add("Name");
            //dt1.Columns.Add("");

            //GridviewControl.GridViewDataBind(this.GridView2, dt1);
            //GridviewControl.ResetGridView(this.GridView2);

            this.GridView3.Attributes.Add("SortExpression", "CityId");
            this.GridView3.Attributes.Add("SortDirection", "ASC");
           
            DataTable dt2 = new DataTable();
            dt2.Columns.Add("CityId");
            dt2.Columns.Add("CenterName");
            dt2.Columns.Add("Name");
            dt2.Columns.Add("");

            GridviewControl.GridViewDataBind(this.GridView3, dt2);
            GridviewControl.ResetGridView(this.GridView3);
        }
    }
    protected void GridViewBind()
    {

        // 获取GridView排序数据列及排序方向
        string sortExpression = this.GridView1.Attributes["SortExpression"];
        string sortDirection = this.GridView1.Attributes["SortDirection"];

        string keyword = txtkeyword.Text.Trim();
        if (CoreTools.ProcessSqlStr(keyword, 0))
        {
            DataSet ds = jtDataAccess.SearchClass(keyword, pager1.PageSize, pager1.CurrentPageIndex, sortExpression, sortDirection);
            if (CoreTools.CheckDataSet(ds))
            {
                // 根据GridView排序数据列及排序方向设置显示的默认数据视图
                if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
                {
                    ds.Tables[0].DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
                }

                // GridView绑定并显示数据

                GridviewControl.GridViewDataBind(this.GridView1, ds.Tables[0]);

                // this.GridView1.DataSource = ds.Tables[0];
                // this.GridView1.DataBind();
                pager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0]["qty"]);
            }
            else
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("ClassId");
                dt.Columns.Add("Code");
                dt.Columns.Add("ClassTypeId");
                dt.Columns.Add("ClassTypeName");
                dt.Columns.Add("Name");


                GridviewControl.GridViewDataBind(this.GridView1, dt);
                pager1.RecordCount = 0;
            }
        }
        else
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            // CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg);
            txtkeyword.Focus();
        }

    }
    protected void selectAll_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("chkID");
            if (selectAll.Checked == true)
            {
                cbox.Checked = true;
            }
            else
            {
                cbox.Checked = false;
            }
        }

    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //首先判断是否是数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FCFDEE'");
            //当鼠标移开时还原背景色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

        }
    }
    protected void pager1_PageChanged(object sender, EventArgs e)
    {
        GridView1.EditIndex = -1;
        GridViewBind();
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        // 从事件参数获取排序数据列
        string sortExpression = e.SortExpression.ToString();

        // 假定为排序方向为“顺序”
        string sortDirection = "ASC";

        // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
        if (sortExpression == this.GridView1.Attributes["SortExpression"])
        {
            //获得下一次的排序状态
            sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
        }

        // 重新设定GridView排序数据列及排序方向
        this.GridView1.Attributes["SortExpression"] = sortExpression;
        this.GridView1.Attributes["SortDirection"] = sortDirection;

        this.GridViewBind();
    }
    protected void ibtnsearch_Click(object sender, ImageClickEventArgs e)
    {
        GridView1.EditIndex = -1;
        GridViewBind();
    }
    protected void btnViewReport_Click(object sender, EventArgs e)
    {
       
        UserType ut = new UserType();//用户自定义类型
        DataTable ClassTableType = ut.GetClassTableType();
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            HiddenField hdfClassId = (HiddenField)GridView1.Rows[i].FindControl("hdfClassId");
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("chkID");
          
            if (cbox != null && cbox.Checked == true)
            {
                DataRow ClassTableTypeRow;
                ClassTableTypeRow = ClassTableType.NewRow();
                ClassTableTypeRow["ClassId"] = hdfClassId.Value;
                ClassTableType.Rows.Add(ClassTableTypeRow);
            }

        }
        ClassTableType.AcceptChanges();

        //DataTable CenterTable = ut.GetCenterTableType();
        //for (int j = 0; j < GridView2.Rows.Count; j++)
        //{
        //    HiddenField hdfCenterId = (HiddenField)GridView2.Rows[j].FindControl("hdfCenterId");
        //    Label lblname = (Label)GridView2.Rows[j].FindControl("lblname");
        //    CheckBox cbox = (CheckBox)GridView2.Rows[j].FindControl("chkID");

        //    if (cbox != null && cbox.Checked == true)
        //    {
        //        DataRow CenterTableRow;
        //        CenterTableRow = ClassTableType.NewRow();
        //        CenterTableRow["CenterId"] = hdfCenterId.Value;
        //        CenterTableRow["NAME"] = lblname.Text;
        //        CenterTable.Rows.Add(CenterTableRow);
        //    }
        //}
        //CenterTable.AcceptChanges();


        DataTable CityTable = ut.GetCityTableType();
        for (int k = 0; k < GridView3.Rows.Count; k++)
        {
            HiddenField hdfCityId = (HiddenField)GridView3.Rows[k].FindControl("hdfCityId");
            Label lblname = (Label)GridView3.Rows[k].FindControl("lblname");
            CheckBox cbox = (CheckBox)GridView3.Rows[k].FindControl("chkID");
            if (cbox != null && cbox.Checked == true)
            {
                DataRow CityTableRow;
                CityTableRow = CityTable.NewRow();
                CityTableRow["CityId"] = hdfCityId.Value;
                CityTableRow["NAME"] = lblname.Text;
                CityTable.Rows.Add(CityTableRow);
            }
        }
        CityTable.AcceptChanges();


        this.ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Local;
        LocalReport localReport = ReportViewer1.LocalReport;
        localReport.ReportPath = "Reports/Emp3/ReportH.rdlc";
        localReport.DataSources.Clear();

        //按所属指挥中心分组
        DataSet ds1 = new DataSet();
        SqlParameter[] para1 = { 
                                  };
        ds1 = DbHelperSQL.ExecProc("uspEmp3ReportH1", para1);
        ReportDataSource rds1 = new ReportDataSource();
        rds1.Name = "DataSet1";
        rds1.Value = ds1.Tables[0];
        localReport.DataSources.Add(rds1);

        //按城市分组
        DataSet ds2 = new DataSet();
        SqlParameter[] para2 = {
                                  };
        ds2 = DbHelperSQL.ExecProc("uspEmp3ReportH2", para2);
        ReportDataSource rds2 = new ReportDataSource();
        rds2.Name = "DataSet2";
        rds2.Value = ds2.Tables[0];
        localReport.DataSources.Add(rds2);

        //按课程名称分组
        DataSet ds3 = new DataSet();
        SqlParameter[] para3 = {
                                    new SqlParameter("@Class",ClassTableType)    
                                  , new SqlParameter("@City",CityTable) 
                                  };
        ds3 = DbHelperSQL.ExecProc("uspEmp3ReportH3", para3);
        ReportDataSource rds3 = new ReportDataSource();
        rds3.Name = "DataSet3";
        rds3.Value = ds3.Tables[0];
        localReport.DataSources.Add(rds3);

        ////Customer
        //DataSet ds4 = new DataSet();             
        //ds4 = DbHelperSQL.Query("SELECT CustomerId,Name FROM dbo.Customer ORDER BY Name; ");
        //ReportDataSource rds4 = new ReportDataSource();
        //rds4.Name = "Customer";
        //rds4.Value = ds4.Tables[0];
        //localReport.DataSources.Add(rds4);

        ////Class
        //DataSet ds5 = new DataSet();
        //ds5 = DbHelperSQL.Query("SELECT ClassId,Name FROM dbo.Class ORDER BY Name; ");
        //ReportDataSource rds5 = new ReportDataSource();
        //rds5.Name = "Class";
        //rds5.Value = ds5.Tables[0];
        //localReport.DataSources.Add(rds5);

        //设置参数
        //ReportParameter[] paras = new ReportParameter[2];
        //paras[0] = new ReportParameter("CustomerId");
        //paras[1] = new ReportParameter("ClassId");          

        //localReport.SetParameters(paras);

        ReportViewer1.LocalReport.Refresh();


       
        GridviewControl.ResetGridView(this.GridView1);
    }
    protected void lbtnselectClass_Click(object sender, EventArgs e)
    {
        divclasslist.Style.Remove("visibility");
        divreport.Style.Add("visibility", "hidden");
        GridView3.Visible = false;
        GridView1.Visible = true;
        ReportViewer1.Visible = false;
       // divcenterlist.Style.Add("visibility", "hidden");
        divcitylist.Style.Add("visibility", "hidden");
        lbtnselectClass.CssClass = "tabpage_selected_title";
        lbtnviewreport.CssClass = "tabpage_noselected_title";
       // lbtnselectCenter.CssClass = "tabpage_noselected_title";
        lbtnselectCity.CssClass = "tabpage_noselected_title";
        GridviewControl.ResetGridView(this.GridView1);
       // GridviewControl.ResetGridView(this.GridView2);
        GridviewControl.ResetGridView(this.GridView3);
    }
    protected void lbtnviewreport_Click(object sender, EventArgs e)
    {
        divreport.Style.Remove("visibility");
        divclasslist.Style.Add("visibility", "hidden");
        GridView3.Visible = false;
        GridView1.Visible = false;
        ReportViewer1.Visible = true;
       // divcenterlist.Style.Add("visibility", "hidden");
        divcitylist.Style.Add("visibility", "hidden");
        lbtnselectClass.CssClass = "tabpage_noselected_title";
        lbtnviewreport.CssClass = "tabpage_selected_title";
       // lbtnselectCenter.CssClass = "tabpage_noselected_title";
        lbtnselectCity.CssClass = "tabpage_noselected_title";
        GridviewControl.ResetGridView(this.GridView1);
        //GridviewControl.ResetGridView(this.GridView2);
        GridviewControl.ResetGridView(this.GridView3);
    }
    //protected void ibtnsearchcenter_Click(object sender, ImageClickEventArgs e)
    //{
    //    GridView2.EditIndex = -1;
    //    BindCenter();
    //}
    //protected void selectAllCenter_CheckedChanged(object sender, EventArgs e)
    //{
    //    for (int i = 0; i <= GridView2.Rows.Count - 1; i++)
    //    {
    //        CheckBox cbox = (CheckBox)GridView2.Rows[i].FindControl("chkID");
    //        if (selectAllCenter.Checked == true)
    //        {
    //            cbox.Checked = true;
    //        }
    //        else
    //        {
    //            cbox.Checked = false;
    //        }
    //    }
    //}

    //protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    //{
    //    GridView2.EditIndex = -1;
    //    BindCenter();

    //}
    //protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    //{
    //    //首先判断是否是数据行
    //    if (e.Row.RowType == DataControlRowType.DataRow)
    //    {
    //        //当鼠标停留时更改背景色
    //        e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FCFDEE'");
    //        //当鼠标移开时还原背景色
    //        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

    //    }
    //}
    //protected void BindCenter()
    //{
    //    // 获取GridView排序数据列及排序方向
    //    string sortExpression = this.GridView2.Attributes["SortExpression"];
    //    string sortDirection = this.GridView2.Attributes["SortDirection"];

    //    string keyword = txtkey.Text.Trim();
    //    if (CoreTools.ProcessSqlStr(keyword, 0))
    //    {
    //        DataSet ds = jtDataAccess.SearchCenter(keyword, AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, sortExpression, sortDirection);
    //        if (CoreTools.CheckDataSet(ds))
    //        {
    //            // 根据GridView排序数据列及排序方向设置显示的默认数据视图
    //            if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
    //            {
    //                ds.Tables[0].DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
    //            }

    //            // GridView绑定并显示数据

    //            GridviewControl.GridViewDataBind(this.GridView2, ds.Tables[0]);
              
    //            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0]["qty"]);

    //            selectAllCenter.Enabled = true;

    //        }
    //        else
    //        {
    //            DataTable dt = new DataTable();
    //            dt.Columns.Add("CenterId");
    //            dt.Columns.Add("Name");
    //            dt.Columns.Add("");

    //            GridviewControl.GridViewDataBind(this.GridView2, dt);
    //            AspNetPager1.RecordCount = 0;

    //            selectAllCenter.Enabled = false;
    //        }
    //    }
    //    else
    //    {
    //        ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
    //        txtkey.Focus();
    //    }

    //}
    protected void lbtnselectCenter_Click(object sender, EventArgs e)
    {
        //divcenterlist.Style.Remove("visibility");
        divclasslist.Style.Add("visibility", "hidden");
        divreport.Style.Add("visibility", "hidden");
        divcitylist.Style.Add("visibility", "hidden");
        lbtnselectClass.CssClass = "tabpage_noselected_title";
        lbtnviewreport.CssClass = "tabpage_noselected_title";
        lbtnselectCity.CssClass = "tabpage_noselected_title";
       // lbtnselectCenter.CssClass = "tabpage_selected_title";
        GridviewControl.ResetGridView(this.GridView1);
       // GridviewControl.ResetGridView(this.GridView2);
        GridviewControl.ResetGridView(this.GridView3);
    }
    protected void selectAllCity_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i <= GridView3.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView3.Rows[i].FindControl("chkID");
            if (selectAllCity.Checked == true)
            {
                cbox.Checked = true;
            }
            else
            {
                cbox.Checked = false;
            }
        }
    }
    protected void BindCity()
    {
        // 获取GridView排序数据列及排序方向
        string sortExpression = this.GridView3.Attributes["SortExpression"];
        string sortDirection = this.GridView3.Attributes["SortDirection"];

        string keyword =txtckey.Text.Trim();
        if (CoreTools.ProcessSqlStr(keyword, 0))
        {
            DataSet ds = jtDataAccess.SearchCity(keyword, AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex, sortExpression, sortDirection);
            if (CoreTools.CheckDataSet(ds))
            {
                // 根据GridView排序数据列及排序方向设置显示的默认数据视图
                if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
                {
                    ds.Tables[0].DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
                }
              
                // GridView绑定并显示数据

                GridviewControl.GridViewDataBind(this.GridView3, ds.Tables[0]);

                AspNetPager2.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0]["qty"]);


                selectAllCity.Enabled = true;
              
            }
            else
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("CityId");
                dt.Columns.Add("CenterName");
                dt.Columns.Add("Name");
                dt.Columns.Add("");

                GridviewControl.GridViewDataBind(this.GridView3, dt);
                AspNetPager2.RecordCount = 0;

                selectAllCity.Enabled = false;
            }
        }
        else
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txtckey.Focus();
        }

    }
    protected void AspNetPager2_PageChanged(object sender, EventArgs e)
    {
        GridView3.EditIndex = -1;
        BindCity();

    }
    protected void ibtnsearchCity_Click(object sender, ImageClickEventArgs e)
    {
        GridView3.EditIndex = -1;
        BindCity();
    }
    protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //首先判断是否是数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FCFDEE'");
            //当鼠标移开时还原背景色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

        }
    }
    protected void lbtnselectCity_Click(object sender, EventArgs e)
    {
        divcitylist.Style.Remove("visibility");
        GridView1.Visible = false;
        GridView3.Visible = true;
        ReportViewer1.Visible = false;
       // divcenterlist.Style.Add("visibility", "hidden");
        divclasslist.Style.Add("visibility", "hidden");
        divreport.Style.Add("visibility", "hidden");
        lbtnselectClass.CssClass = "tabpage_noselected_title";
        lbtnviewreport.CssClass = "tabpage_noselected_title";
       // lbtnselectCenter.CssClass = "tabpage_noselected_title";
        lbtnselectCity.CssClass = "tabpage_selected_title";
        GridviewControl.ResetGridView(this.GridView1);
       // GridviewControl.ResetGridView(this.GridView2);
        GridviewControl.ResetGridView(this.GridView3);
    }
}